home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ProgressBar.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  15.6 KB  |  545 lines

  1. package symantec.itools.awt.util;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.awt.Color;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.Rectangle;
  11. import symantec.itools.awt.AlignStyle;
  12. import symantec.itools.awt.BevelStyle;
  13.  
  14.  
  15. /**
  16.  * ProgressBar component.
  17.  * Creates a bar that displays a percentage.
  18.  * Commonly used to indicate the percentage completed of a
  19.  * lengthy task.
  20.  
  21.  * @version 1.0, Nov 26, 1996
  22.  * @author Symantec
  23.  */
  24.  
  25.  
  26. public class ProgressBar
  27.     extends Canvas
  28.     implements BevelStyle, AlignStyle
  29. {
  30.     /**
  31.      * Border Indent constant INDENT_ZERO.
  32.      */
  33.     public static final int INDENT_ZERO = 0;
  34.  
  35.     /**
  36.      * Border Indent constant INDENT_ONE.
  37.      */
  38.     public static final int INDENT_ONE = 1;
  39.  
  40.     /**
  41.      * Border Indent constant INDENT_TWO.
  42.      */
  43.     public static final int INDENT_TWO = 2;
  44.  
  45.     private int align;
  46.     private int type;
  47.     private int progress = 0;
  48.     private Color color1;
  49.     private Color color2;
  50.     private Color textColor;
  51.     private Color borderedColor;
  52.     private Color progressColor;
  53.     private boolean bShowProgress = true;
  54.     private boolean bDrawBoxes = false;
  55.     private int boxWidth = 10;
  56.     private int gapWidth = 2;
  57.  
  58.     // declared here for faster execution
  59.     private FontMetrics fm;
  60.     private int xTemp;
  61.     private int yTemp;
  62.     private int indent = INDENT_ZERO;
  63.  
  64.     /**
  65.      * Constructs a progress bar with a centered progress %, no border and zero indentation.
  66.      */
  67.     public ProgressBar()
  68.     {
  69.         this(ALIGN_CENTERED, BEVEL_NONE, INDENT_ZERO);
  70.     }
  71.  
  72.     /**
  73.      * Constructs a progress bar with the style and indentation variables specified.
  74.      * The default parameters are the bordered color and progress % color as black, the progress bar color
  75.      * as blue.
  76.      * @param align progress bar alignment style
  77.      * @param bevel progress bar bevel style
  78.      * @param indent border indent INDENT_ZERO, INDENT_ONE, or INDENT_TWO
  79.      */
  80.     public ProgressBar(int align, int bevel, int indent)
  81.     {
  82.         // set default values
  83.         textColor = Color.black;
  84.         borderedColor = Color.black;
  85.         progressColor = Color.blue;
  86.  
  87.         // process arguments
  88.         setBorderIndent(indent, false);
  89.         setBevelStyle(bevel);
  90.         setAlignStyle(align);
  91.     }
  92.  
  93.     /**
  94.      * Set the color that the progress bar will be drawn in.
  95.      * @param color the color that the progress bar will be drawn in.
  96.      * @see #getProgressBarColor
  97.      */
  98.     public void setProgressBarColor(Color c)
  99.     {
  100.         progressColor = c;
  101.         invalidate();
  102.     }
  103.  
  104.     /**
  105.      * Get the color that the progress bar will be drawing in.  The default color is blue.
  106.      * @return Color that the Progress Bar will be drawn in.
  107.      * @see #setProgressBarColor
  108.      */
  109.     public Color getProgressBarColor()
  110.     {
  111.         return progressColor;
  112.     }
  113.  
  114.     /**
  115.      * Set the color of the percentage completed text in the progress bar.
  116.      * @param c the color that the progress bar text will be drawn in.
  117.      * @see #getProgressBarTextColor
  118.      */
  119.     public void setProgressBarTextColor(Color c)
  120.     {
  121.         textColor = c;
  122.         invalidate();
  123.     }
  124.  
  125.     /**
  126.      * Get the color of the percentage completed text in the progress bar.  The default color is black.
  127.      * @return Color that the progress bar text will be drawn in.
  128.      * @see #setProgressBarTextColor
  129.      */
  130.     public Color getProgressBarTextColor()
  131.     {
  132.         return textColor;
  133.     }
  134.  
  135.     /**
  136.      * Get the boolean that controls the drawing of the progress bar as a series of boxes.
  137.      * The default value is false.
  138.      * @return boolean indicating if the bar should be composed of multiple boxes.
  139.      * @see #setDrawBoxes
  140.      */
  141.     public boolean getDrawBoxes()
  142.     {
  143.         return bDrawBoxes;
  144.     }
  145.  
  146.     /**
  147.      * Set the boolean that controls the drawing of the progress bar as a series of boxes.
  148.      * @param b indicating if the bar should be composed of multiple boxes.
  149.      * @see #getDrawBoxes
  150.      */
  151.     public void setDrawBoxes(boolean b)
  152.     {
  153.         bDrawBoxes = b;
  154.         invalidate();
  155.     }
  156.  
  157.     /**
  158.      * Set the width of the boxes drawn for the progress bar.
  159.      * @param i the pixel width of the boxes in the progress bar.
  160.      * @see #getBoxWidth
  161.      */
  162.     public void setBoxWidth(int i)
  163.     {
  164.         boxWidth = i;
  165.         invalidate();
  166.     }
  167.  
  168.     /**
  169.      * Get the width of the boxes drawn for the progress bar.  The default value is 8 pixels.
  170.      * @return int the pixel width of the boxes in the progress bar.
  171.      * @see #setBoxWidth
  172.      */
  173.     public int getBoxWidth()
  174.     {
  175.         return boxWidth;
  176.     }
  177.  
  178.     /**
  179.      * Set the gap width between boxes drawn for the progress bar.
  180.      * @param i the pixel width of the boxes in the progress bar.
  181.      * @see #getBoxWidth
  182.      * @see #getGapWidth
  183.      */
  184.     public void setGapWidth(int i)
  185.     {
  186.         gapWidth = i;
  187.         invalidate();
  188.     }
  189.  
  190.     /**
  191.      * Get the gap width of the boxes drawn for the progress bar.  The default value is 2 pixels.
  192.      * @return i the pixel width of the gap between boxes in the progress bar.
  193.      * @see #setBoxWidth
  194.      * @see #setGapWidth
  195.      */
  196.     public int getGapWidth()
  197.     {
  198.         return gapWidth;
  199.     }
  200.  
  201.     /**
  202.      * Get the boolean that controls the display of the progress number as a percentage.  The default value is true
  203.      * @return boolean indicating if the number should be displayed.
  204.      * @see #setShowProgress
  205.      */
  206.     public boolean getShowProgress()
  207.     {
  208.         return bShowProgress;
  209.     }
  210.  
  211.     /**
  212.      * Set the boolean that controls the display of the progress number as an percentage.
  213.      * @param b indicating if the progress percentage should be displayed.
  214.      * @see #getShowProgress
  215.      */
  216.     public void setShowProgress(boolean b)
  217.     {
  218.         bShowProgress = b;
  219.         invalidate();
  220.     }
  221.  
  222.     /**
  223.      * Set the style of the progress bar's alignment.
  224.      * @param style numeric value indicating the progress bar's alignment
  225.      * @see #getAlignStyle
  226.      */
  227.     public void setAlignStyle(int style)
  228.     {
  229.         align = style;
  230.         invalidate();
  231.     }
  232.  
  233.     /**
  234.      * Get the current style of the progress bar's alignment
  235.      * @return int the style numeric value indicating progress bar's alignment
  236.      * @see #setAlignStyle
  237.      */
  238.     public int getAlignStyle()
  239.     {
  240.         return align;
  241.     }
  242.  
  243.     /**
  244.      * Set the style of the progress bar's border.
  245.      * @param style numeric value indicating the progress bar's style
  246.      * @see #getBevelStyle
  247.      */
  248.     public void setBevelStyle(int style)
  249.     {
  250.         type = style;
  251.  
  252.         switch (type)
  253.         {
  254.             case BEVEL_LOWERED:
  255.                 color1 = Color.black;
  256.                 color2 = Color.white;
  257.                 break;
  258.  
  259.             case BEVEL_LINE:
  260.                 color1 = borderedColor;
  261.                 color2 = borderedColor;
  262.                 break;
  263.  
  264.             case BEVEL_RAISED:
  265.                 color1 = Color.white;
  266.                 color2 = Color.black;
  267.                 break;
  268.  
  269.             default: // catches any bogus type, paint(...) relies on color1 being null
  270.                 color1 = color2 = null;
  271.                 break;
  272.         }
  273.  
  274.         invalidate();
  275.     }
  276.  
  277.     /**
  278.      * Get the current style of the progress bar's border
  279.      * @return int the style numeric value indicating progress bar's border, such as BEVEL_LOWERED
  280.      * @see #setBevelStyle
  281.      */
  282.     public int getBevelStyle()
  283.     {
  284.         return type;
  285.     }
  286.  
  287.     /**
  288.      * Set the border indent amount.
  289.      * @param indent INDENT_ZERO, INDENT_ONE or INDENT_TWO
  290.      * @see #getBorderIndent
  291.      */
  292.     public void setBorderIndent(int indent)
  293.     {
  294.         setBorderIndent(indent, true);
  295.     }
  296.  
  297.     private void setBorderIndent(int indent, boolean bRepaint)
  298.     {
  299.         if (indent < INDENT_ZERO)
  300.             this.indent = INDENT_ZERO;
  301.         else if (indent > INDENT_TWO)
  302.             this.indent = INDENT_TWO;
  303.         else
  304.             this.indent = indent;
  305.  
  306.         if (bRepaint)
  307.             repaint();
  308.     }
  309.  
  310.     /**
  311.      * Get the border indent amount.  The default value is INDENT_ZERO.
  312.      * @return int INDENT_ZERO, INDENT_ONE or INDENT_TWO
  313.      * @see #setBorderIndent
  314.      */
  315.     public int getBorderIndent()
  316.     {
  317.         return indent;
  318.     }
  319.  
  320.     /**
  321.      * Set the color for the border of BEVEL_LINE style ProgressBar.  This will not be used for other styles.
  322.      * @param color color that the border will be drawn in.
  323.      * @see #getBorderedColor
  324.      */
  325.     public void setBorderedColor(Color color)
  326.     {
  327.         borderedColor = color;
  328.         if (type == BEVEL_LINE)
  329.         {
  330.             color1 = color;
  331.             color2 = color;
  332.         }
  333.         invalidate();
  334.     }
  335.  
  336.     /**
  337.      * Get the color for the border of BEVEL_LINE style ProgressBar.  This will not be used for other styles.
  338.      * @return Color that the border will be drawn in.
  339.      * @see #setBorderedColor
  340.      */
  341.     public Color getBorderedColor()
  342.     {
  343.         return borderedColor;
  344.     }
  345.  
  346.     /**
  347.      * Set the percentage complete for the process being tracked.
  348.      * @param p percentage complete as an integer
  349.      */
  350.     public void updateProgress(int p)
  351.     {
  352.         if (p < 0) p = 0;
  353.         if (p > 100) p = 100;
  354.         progress = p;
  355.         repaint();
  356.     }
  357.  
  358.     /**
  359.      * Set the current percentage complete.
  360.      * @param p percentage complete
  361.      * @see #getValue
  362.      */
  363.     public void setValue(int p)
  364.     {
  365.         updateProgress(p);
  366.     }
  367.  
  368.     /**
  369.      * Get the current percentage complete.
  370.      * @return int - current percentage complete
  371.      * @see #setValue
  372.      */
  373.     public int getValue()
  374.     {
  375.         return progress;
  376.     }
  377.  
  378.     /**
  379.      * Handles redrawing of this component on the screen.
  380.      * This is a standard Java AWT method which gets called by the Java
  381.      * AWT (repaint()) to handle repainting this component on the screen.
  382.      * The graphics context clipping region is set to the bounding rectangle
  383.      * of this component and its <0,0> coordinate is this component's
  384.      * top-left corner.
  385.      * Typically this method paints the background color to clear the
  386.      * component's drawing space, sets graphics context to be the foreground
  387.      * color, and then calls paint() to draw the component.
  388.      *
  389.      * It is overridden here to reduce flicker by eliminating the uneeded
  390.      * clearing of the background.
  391.      *
  392.      * @param g the graphics context
  393.      * @see java.awt.Component#repaint
  394.      * @see #paint
  395.      */
  396.     public void update(Graphics g)
  397.     {
  398.         paint(g);
  399.     }
  400.  
  401.  
  402.     /**
  403.      * Paints this component using the given graphics context.
  404.      * This is a standard Java AWT method which typically gets called
  405.      * by the AWT to handle painting this component. It paints this component
  406.      * using the given graphics context. The graphics context clipping region
  407.      * is set to the bounding rectangle of this component and its <0,0>
  408.      * coordinate is this component's top-left corner.
  409.      *
  410.      * @param g the graphics context used for painting
  411.      * @see java.awt.Component#repaint
  412.      * @see #update
  413.      */
  414.     public void paint(Graphics g)
  415.     {
  416.         Rectangle r = bounds();
  417.         Color c = g.getColor();
  418.  
  419.         if (color1 != null)
  420.         {
  421.             g.clipRect(0, 0, r.width, r.height);
  422.  
  423.             // top
  424.             g.setColor(color1);
  425.             g.drawLine(1+indent, indent, r.width-3-indent, indent);
  426.  
  427.             // bottom
  428.             g.setColor(color2);
  429.             g.drawLine(1+indent, r.height-1-indent, r.width-3-indent, r.height-1-indent);
  430.  
  431.             // left
  432.             g.setColor(color1);
  433.             g.drawLine(indent, indent, indent, r.height-1-indent);
  434.  
  435.             // right
  436.             g.setColor(color2);
  437.             g.drawLine(r.width-2-indent, indent, r.width-2-indent, r.height-1-indent);
  438.  
  439.             g.clipRect(1+indent, 1+indent, r.width-3-indent, r.height-2-indent);
  440.             yTemp = 1 + indent;
  441.         }
  442.         else
  443.         {
  444.             g.setColor(getBackground());
  445.             g.drawRect(indent,indent, r.width-2-indent, r.height-1-indent);
  446.             g.clipRect(2, 1, r.width-3, r.height-2);
  447.             yTemp = 1;
  448.         }
  449.  
  450.         //progress area and shading
  451.         g.setColor(progressColor);
  452.         if (bDrawBoxes)
  453.         {
  454.             int totalWidth = (boxWidth + gapWidth);
  455.             int pctDrawn = boxWidth * 100 / r.width;
  456.             int widDrawn = 0;
  457.             int i = 0;
  458.             while (pctDrawn < progress)
  459.             {
  460.                 g.setColor(progressColor);
  461.                 g.fillRect((totalWidth * i), 1, boxWidth, r.height - 2);
  462.                 g.setColor(getBackground());
  463.                 g.fillRect((totalWidth * i) + boxWidth, 1, gapWidth, r.height - 2);
  464.                 widDrawn = (totalWidth * ++i);
  465.                 pctDrawn = (widDrawn + boxWidth) * 100 / r.width;
  466.             }
  467.             g.setColor(getBackground());
  468.             g.fillRect(widDrawn+1, 1, r.width - widDrawn - 1, r.height - 1);
  469.         }
  470.         else
  471.         {
  472.             g.fillRect(1, 1, ((r.width) * progress) / 100, r.height - 1);
  473.             g.setColor(getBackground());
  474.             g.fillRect(1 + ((r.width) * progress) / 100, 1, (r.width - (((r.width) * progress)) / 100), r.height - 1);
  475.         }
  476.  
  477.         // text
  478.         if (bShowProgress)
  479.         {
  480.             fm = getFontMetrics(getFont());
  481.             yTemp = ((r.height + fm.getAscent()) / 2) - 2;
  482.  
  483.             g.setColor(textColor);
  484.             String sz = (Integer.toString(progress) + "%");
  485.             xTemp = (r.width - fm.stringWidth(sz)) / 2;
  486.             switch (align)
  487.             {
  488.                 case ALIGN_LEFT:
  489.                     if (type == BEVEL_NONE)
  490.                         g.drawString(sz, 4, yTemp);
  491.                     else
  492.                         g.drawString(sz, 8, yTemp);
  493.                     break;
  494.  
  495.                 case ALIGN_RIGHT:
  496.                     xTemp = r.width - fm.stringWidth(sz);
  497.                     if (type == BEVEL_NONE)
  498.                         g.drawString(sz, xTemp - 6, yTemp);
  499.                     else
  500.                         g.drawString(sz, xTemp - 10, yTemp);
  501.                     break;
  502.  
  503.                 case ALIGN_CENTERED:
  504.                     xTemp = (r.width - fm.stringWidth(sz)) / 2;
  505.                     if (type == BEVEL_NONE)
  506.                         g.drawString(sz, xTemp, yTemp);
  507.                     else
  508.                         g.drawString(sz, xTemp, yTemp);
  509.                     break;
  510.             }
  511.         }
  512.         // reset color
  513.         g.setColor(c);
  514.     }
  515.  
  516.     /**
  517.      * Returns the recommended dimensions to properly display this component.
  518.      * This is a standard Java AWT method which gets called to determine
  519.      * the recommended size of this component.
  520.      *
  521.      * @see #minimumSize
  522.      */
  523.     public Dimension preferredSize()
  524.     {
  525.         Dimension s = size();
  526.         Dimension m = minimumSize();
  527.  
  528.         return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
  529.     }
  530.  
  531.     /**
  532.      * Returns the minimum dimensions to properly display this component.
  533.      * This is a standard Java AWT method which gets called to determine
  534.      * the minimum size of this component.
  535.      *
  536.      * @see #preferredSize
  537.      */
  538.     public Dimension minimumSize()
  539.     {
  540.         fm = getFontMetrics(getFont());
  541.  
  542.         return new Dimension(50, fm.getHeight() + 4);
  543.     }
  544. }
  545.